package com.joven.product.api.facade.api.web;

import com.joven.infra.common.model.PageResult;
import com.joven.infra.common.model.Response;
import com.joven.product.api.business.service.ProductService;
import com.joven.product.api.business.service.dto.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 商品Controller
 *
 * @author Joven
 * @date 2022-01-06 18:21:27
 */
@RestController
@RequestMapping("/product")
@Api(tags = "商品")
@Validated
public class ProductController {

    @Autowired
    private ProductService service;

    /**
     * 分页查询
     *
     * @param pageQuery
     * @return
     */
    @ApiOperation("分页查询")
    @PostMapping("/getByPage")
    public Response<PageResult<ProductDTO>> getByPage(@ApiParam @RequestBody ProductPageQueryDTO pageQuery) {
        return Response.success(service.getByPage(pageQuery));
    }

    @ApiOperation("根据id查询详情")
    @GetMapping("/getDetailById")
    public Response<ProductDetailDTO> getDetailById(@NotNull(message = "id不能为空") Long id) {
        return Response.success(service.getDetailById(id));
    }

    /**
     * 创建
     *
     * @param createDTO
     * @return
     */
    @ApiOperation("创建")
    @PostMapping("/create")
    public Response<Boolean> create(@ApiParam @RequestBody @Validated ProductCreateDTO createDTO) {
        return Response.success(service.create(createDTO));
    }

    /**
     * 编辑
     *
     * @param editDTO
     * @return
     */
    @ApiOperation("编辑")
    @PostMapping("/edit")
    public Response<Boolean> edit(@ApiParam @RequestBody @Valid ProductEditDTO editDTO) {
        return Response.success(service.edit(editDTO));
    }
}
